// pages/index/activeDetails/activeDetails.js
var WxParse = require('../../../wxParse/wxParse.js');
import {
  getIndexPageData,
  loginWithCode,
  activityDetail,
  collectGoods,
} from "../../../api.js"
Page({
  data: {
    activeDetail: {},
    isCollect: false,
    activeId: '',
    optionsId: '',
  },

  onLoad: function (options) {
    let _this = this;
    let token = getApp().globalData.Store.token;
    let id = options.id;
    this.setData({
      optionsId: options.id
    })
    activityDetail(token, id).then(res => {
      if (res.data.code == 666) {
        _this.setData({
          activeDetail: res.data.data[0],
          activeId: id
        })
        WxParse.wxParse('content', 'html', res.data.data[0].content, _this, 5)
      } else if (res.data.code == 111111) {
        _this.checkUserInfo()
        _this.onShow()
      }

    })
    this.checkUserInfo()
  },
  onShow() {
    getApp().startpage(new Date().getTime())
    let _this = this;
    let token = getApp().globalData.Store.token;
    let id = _this.data.optionsId;
    activityDetail(token, id).then(res => {
      if (res.data.code == 666) {
        _this.setData({
          activeDetail: res.data.data[0],
          activeId: id
        })
        WxParse.wxParse('content', 'html', res.data.data[0].content, _this, 5)
      }
    })
    this.checkUserInfo()
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
  skipDetail(e) {
    wx.navigateTo({
      url: '/pages/public/productdetail/productdetail?id=' + e.currentTarget.dataset.id + '&enter_type=5&position=' + e.currentTarget.dataset.ind,
    })
  },
  collect(e) {
    let product_id = String(e.currentTarget.dataset.id);
    let token = getApp().globalData.Store.token;
    let index = e.currentTarget.dataset.ind;
    collectGoods({ product_id, token }).then(res => {
      if (res.data.code == 666) {
        let isCollect = 'activeDetail.product_list[' + index + '].user_wishlist'
        this.setData({
          [isCollect]: this.data.activeDetail.product_list[index].user_wishlist == 1 ? 0 : 1
        })
        if (this.data.activeDetail.product_list[index].user_wishlist == 0) {
          wx.showToast({
            title: '已取消收藏',
            icon: 'none',
          })
        } else if (this.data.activeDetail.product_list[index].user_wishlist == 1) {
          wx.showToast({
            title: '已成功收藏',
            icon: 'none',
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none',
        })
        this.checkUserInfo()
      }
    })
  },
  onShareAppMessage: function () {
    return {
      title: this.data.activeDetail.title,
      imageUrl: this.data.activeDetail.main_picture_url,
      path: '/pages/homepage/activeDetails/activeDetails?id=' + this.data.activeId,

    }
  },
  // 授权查询
  checkUserInfo() {
    const xp = this;
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.navigateTo({
            url: '/pages/public/shouquan/shouquan',
          })
        } else {
          wx.getUserInfo({
            success(res) {
              xp.loginWithCode(res.userInfo)
            }
          })
        }
      }
    })
  },
  //登陆
  loginWithCode(userInfo) {
    const xp = this
    wx.login({
      success({
        code
      }) {
        loginWithCode({
          ...userInfo,
          code,
          business_id: getApp().globalData.businessId
        }).then(res => {
          if (res.data.code === 666) {
            getApp().globalData.Store = res.data.data
          }
        })
      }
    })
  },
  return_index() {
    wx.switchTab({
      url: '/pages/tabBar/index/index'
    })
    getApp().endpage(new Date().getTime())
  }
})